import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/train/train_user_check_recoder_list_page.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import 'controller/director_work_train_list_controller.dart';
import 'director_work_train_details_page.dart';

///班主任管理的培训
class DirectorWorkTrainListPage extends StatefulWidget {
  final String tag;

  final bool shrinkWrap;

  const DirectorWorkTrainListPage(
      {Key key, this.shrinkWrap = false, @required this.tag})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _DirectorWorkTrainListPageState();
  }
}

class _DirectorWorkTrainListPageState extends State<DirectorWorkTrainListPage>
    with SingleTickerProviderStateMixin {
  DirectorTrainListController _historyController;
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _historyController = Get.put(
      DirectorTrainListController(),
      tag: widget.tag,
    );
    _tabController = new TabController(length: 2, vsync: this);

    Future.delayed(Duration.zero, () async {
      await _historyController.requestListFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("培训列表"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (DirectorTrainListController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _historyController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    List<TrainBean> _list = _historyController.getList();

    if (_list == null || _list.length == 0) {
      return Center(
        child: NoDataWidget(
          text: "暂无管理的培训",
          clickCallBack: () {
            _historyController.requestListFunction(click: true);
          },
        ),
      );
    }
    if (widget.shrinkWrap) {
      return buildListView();
    }
    return RefreshIndicator(
      onRefresh: () async {
        await _historyController.requestListFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: buildListView(),
    );
  }

  AnimatedSwitcher buildListViewAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(
      ///动画执行切换时间
      duration: const Duration(milliseconds: 200),

      ///动画构建器 构建指定动画类型
      transitionBuilder: (Widget child, Animation<double> animation) {
        //执行缩放动画
        return FadeTransition(child: child, opacity: animation);
      },

      ///执行动画的子 Widget
      ///只有子 Widget 被切换时才会触发动画
      child: Container(
        key: UniqueKey(),
        child: buildMyListWidget(),
      ),
    );
  }

  int _currentIndex = 0;

  buildMyListWidget() {
    if (_currentIndex == 0) {
      return buildListView();
    }
    return Padding(
      padding: EdgeInsets.only(left: 10),
    );
  }

  buildListView() {
    List<TrainBean> _list = _historyController.getList();
    return ListView.builder(
      padding: EdgeInsets.zero,
      shrinkWrap: widget.shrinkWrap,
      physics: widget.shrinkWrap
          ? NeverScrollableScrollPhysics()
          : BouncingScrollPhysics(),
      //条目个数
      itemCount: _list.length,
      //每个子条目的样式
      itemBuilder: (BuildContext context, int index) {
        TrainBean trainBean = _list[index];

        String trainLineStr = "";
        String trainLine = trainBean.trainLine;
        if (trainLine == "offline") {
          trainLineStr = "线下";
        } else {
          trainLineStr = "线上课程学习";
        }

        int trainStatus = trainBean.trainStatus;

        String statuesStr = "";
        Color textColor = Colors.blueGrey;
        if (trainStatus == 5) {
          textColor = Colors.green;
          statuesStr = "进行中";
        } else if (trainStatus == 6) {
          statuesStr = "已完成";
          textColor = Colors.green;
        } else if (trainStatus == 9) {
          statuesStr = "未开始";
          textColor = Colors.grey;
        } else if (trainStatus == 102) {
          statuesStr = "培训未开始";
          textColor = Colors.grey;
        } else if (trainStatus == 101) {
          statuesStr = "培训已结束";
          textColor = Colors.grey;
        }

        return GestureDetector(
          onTap: () {
            if (widget.tag == "maine2") {
              Get.to(() => TrainUserCheckRecoderListPage(
                    tag: trainBean.trainId.toString(),
                    trainId: trainBean.trainId,
                  ));
              return;
            }
            Get.to(() => DirectorWorkTrainDetailsPage(
                  tag: trainBean.trainId.toString(),
                  trainId: trainBean.trainId,
                ));
          },
          child: Container(
            color: Colors.white,
            margin: EdgeInsets.only(bottom: 10),
            padding: EdgeInsets.all(12),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  children: [
                    Image.asset(
                      "assets/images/3.0x/director_3.png",
                      width: 18,
                      height: 18,
                    ),
                    SizedBox(
                      width: 8,
                    ),
                    Expanded(
                      child: Text(
                        trainBean.trainName,
                        style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w600),
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 14,
                    )
                  ],
                ),
                SizedBox(height: 8),
                Row(
                  children: [
                    Text("培训时间:"),
                    Text(
                      "${trainBean.startStudyTime}  ~ ${trainBean.endStudyTime} ",
                      style: TextStyle(
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
                Row(
                  children: [
                    Text("$trainLineStr"),
                    Expanded(
                      child: Text(
                        "${statuesStr} ",
                        textAlign: TextAlign.end,
                        style: TextStyle(fontSize: 14, color: textColor),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
